package com.ray.auth.system.service.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.flyong.cache.query.cache.CacheConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.ray.auth.system.service.LogService;
import com.ray.auth.system.table.entity.Logs;
import com.ray.auth.system.table.entity.LogsExample;
import com.ray.auth.system.table.entity.LogsExample.Criteria;
import com.ray.auth.system.table.mapper.LogsMapper;
import com.xiaoleilu.hutool.util.StrUtil;
import com.zjhc.flyhong.common.function.enums.DeleteEnum;
import com.zjhc.flyhong.common.function.exception.BussinessException;
import com.zjhc.flyhong.common.function.operator.LoginUser;
import com.zjhc.flyhong.common.function.operator.PageDto;
import com.zjhc.flyhong.common.util.UUIDUtil;
import com.zjhc.flyhong.common.web.ErrorMsg;
import com.zjhc.flyhong.common.web.Result;
import com.zjhc.flyhong.common.web.ResultHelper;
import com.zjhc.flyhong.common.web.SuccessMsg;
import com.zjhc.flyhong.common.web.base.BaseService;
/**
 * <h1>系统权限应用  系统日志  服务实现</h1>
 * @创建人 ray
 * @创建时间 2017-06-10
 * @版本 1.0
 * @重要修改备注 
 * 
 */
@Service
public class LogServiceImpl extends BaseService implements LogService {
	/**日志对象**/
	private static Logger logger = Logger.getLogger(LogServiceImpl.class);
	/**************实现类  其他对象 引用  开始***********************/
	/**应用数据库操作对象**/
	@Autowired
	private LogsMapper LogsMapper;
	
	/**************实现类  其他对象 引用  结束***********************/
	/**
	 * 查询所有从系统日志
	 * @param params 查询参数
	 * @param page 分页信息
	 * @param loginUser 登录用户
	 * @return
	 */
	@CacheConfig(key="LogsList",query=true,paramsSize=2,expire=1800)
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public PageDto<Logs> queryLogs(Logs params, PageDto<Logs> page, LoginUser loginUser) {
		LogsExample example = new LogsExample();
		Criteria criteria =  example.createCriteria();
		if(!loginUser.isSuperManager()){
			criteria.andTenantIdEqualTo(loginUser.getTenantId());
		}
		//登录名称
		if(StrUtil.isNotBlank(params.getLoginName())){
			criteria.andLoginNameLike(params.getLoginName()+"%");
		}
		//操作人
		if(StrUtil.isNotBlank(params.getOperatorUser())){
			criteria.andOperatorUserLike(params.getOperatorUser()+"%");
		}
		
		//操作类别
		if(StrUtil.isNotBlank(params.getOperatorType())){
			criteria.andOperatorTypeLike(params.getOperatorType()+"%");
		}
		
		criteria.andIsDeleteEqualTo(DeleteEnum.USE.getValue());
		this.setExamplePage(example, page);
		page.setTotal(LogsMapper.countByExample(example));
		page.setRecords(LogsMapper.selectByExample(example));
		return page;
	}
	
	 /**
     * 逻辑删除 系统日志数据
     * @param id
     * @param loginUser
     * @return
     */
	@CacheConfig(key="Logs",paramsSize=1,delete=true, clearKey="LogsList")
	@Transactional
	public Result<Object> deleteLogs(String id, LoginUser loginUser) {
		Result<Object> result = null;
		result = new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		Logs record = new Logs();
		record.setId(id);
		record.setIsDelete(DeleteEnum.DELETE.getValue());
		this.setUserForUpdate(record, loginUser);
		int opCount = LogsMapper.updateByPrimaryKeySelective(record);
		if(opCount != 1){
			logger.error("LogsServiceImpl=>deleteLogs is error");
			throw new BussinessException(-1, ErrorMsg.DELETE_ERROR);
		}
		return result;
	}
	
	/**
	 * 新增系统日志
	 * @param record
	 * @param user
	 * @return
	 */
	@Transactional
	public Result<Object> saveLogs(Logs entity, LoginUser loginUser) {
		entity.setTenantId(loginUser.getTenantId());
		entity.setId(UUIDUtil.getUUID());
		entity.setIsDelete(DeleteEnum.USE.getValue());
		this.setUserForAdd(entity, loginUser);
		//执行成功数量	
		int exeCount = LogsMapper.insertSelective(entity);
		if(exeCount != 1)return ResultHelper.getError();
		return ResultHelper.getSuccess();
	}
	
	/**
	 * 修改系统日志
	 * @param record
	 * @param user
	 * @return
	 */

	@CacheConfig(key="Logs",paramsSize=1,update=true,id="LogsId", clearKey="LogsList")
	@Transactional
	public Result<Object> updateLogs(Logs record, LoginUser loginUser) {
		Result<Object> result = null;
		result = new Result<Object>(0,SuccessMsg.OP_SUCCESS);
		this.setUserForUpdate(record, loginUser);
		int opCount = LogsMapper.updateByPrimaryKeySelective(record);
		if(opCount != 1){
			logger.error("LogsServiceImpl=>updateLogs is error");
			throw new BussinessException(-1, ErrorMsg.UPDATE_ERROR);
		}
		return result;
	}
	
	/**
	 * 通过系统日志类型查询
	 * @param menuId
	 * @return
	 */
	
	@CacheConfig(key="LogsList",paramsSize=1,query=true,expire=24*60*60)
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public List<Logs> queryLogssByLogsType(String typeId) {
		Logs record = new Logs();
		record.setId(typeId);
		record.setIsDelete(DeleteEnum.USE.getValue());
		return LogsMapper.selectByRecord(record);
	}

	
	@CacheConfig(key="Logs",paramsSize=1,query=true,expire=1800)
	@Transactional(propagation=Propagation.NOT_SUPPORTED)
	public Logs queryLogs(String id, LoginUser user) {
		return LogsMapper.selectByPrimaryKey(id);
	}

	
	
	
}
